package com.zyw.service.order.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyw.entity.Order;
import com.zyw.from.OrderFrom;
import com.zyw.mapper.order.OrderMapper;
import com.zyw.service.order.OrderService;
import com.zyw.util.GenerateOrdersIDUtil;
import com.zyw.util.UnitConversionUtil;
import com.zyw.vo.LayUITableVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;


@Slf4j
@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    public OrderMapper orderMapper;

    @Override
    public LayUITableVO<List<Order>> findPage(Map<String, Object> map) {

        PageHelper.startPage((int) map.get("page"),(int) map.get("limit"));

        List<Order> list = orderMapper.findPage(map);
        for (Order order : list) {
            order.setOrderDate(UnitConversionUtil.conversionDate(order.getOrderDate()));
            order.setRequireDate(UnitConversionUtil.conversionDate(order.getRequireDate()));

            if (order.getStatus()==0){
                order.setStatusString("订单取消");
            }else if (order.getStatus() == 1){
                order.setStatusString("未开始");
            } else if (order.getStatus() == 2){
                order.setStatusString("准备中");
            }else if (order.getStatus() == 3){
                order.setStatusString("已开始");
            }else if (order.getStatus() == 4){
                order.setStatusString("已完成");
            }



        }

        PageInfo<Order> pageInfo = new PageInfo<>(list);

        LayUITableVO<List<Order>> vo = new LayUITableVO<>();
        vo.setCount(pageInfo.getTotal());
        vo.setData(pageInfo.getList());

        return vo;
    }

    @Override
    public Order selectById(String id) {

        if (null == id ||"".equals(id)){
            return null;
        }

        Order order = orderMapper.selectByPrimaryKey(id);
        order.setOrderDate(UnitConversionUtil.conversionDate(order.getOrderDate()));
        order.setRequireDate(UnitConversionUtil.conversionDate(order.getRequireDate()));

        return order;
    }

    @Override
    @Transactional
    public void update(Order order) {
        orderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    @Transactional
    public void save(Order order) {

        order.setId(GenerateOrdersIDUtil.generateOrdersID(order.getClientId()));

        orderMapper.insertSelective(order);
    }

    @Override
    @Transactional
    public void delelteById(String id) {

        orderMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<Order> selectByStatus(int i) {
        Example example = new Example(Order.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("status",i);

        return orderMapper.selectByExample(example);
    }
}
